package com.hong.utils.executors;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Auther: Administrator
 * @Date: 2018/7/27 19:57
 * @Description:
 */
public class ExecutorServiceExample4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        testInvokeAny();
//        testInvokeAll();
        testSubmitRunnable();
    }

    /**
     * Question:
     * When the result returned,other callable will be keep on process?
     * <p>
     * Answer:
     * other callcabe will e canceled.
     * </p>
     * Note:
     * The invokeAny will be blocked caller.
     * @throws ExecutionException
     * @throws InterruptedException
     */
    private static void testInvokeAny() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        List<Callable<Integer>> collectlist = IntStream.range(0, 5).boxed().map(
                i -> (Callable<Integer>) () -> {
            TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(20));
                    System.out.println(Thread.currentThread().getName()+" : "+i);
            return i;
        }).collect(Collectors.toList());

        Integer value = executorService.invokeAny(collectlist);
        System.out.println("==============finished.===============");
        System.out.println(value);


    }

    /**
     * {@link ExecutorService#invokeAll(Collection,long,TimeUnit)}
     */
    private static void testInvokeAnyTimeOut(){
                ExecutorService executorService = Executors.newFixedThreadPool(10);
    }

    private static void testInvokeAll() throws InterruptedException {
        ExecutorService executorservice = Executors.newFixedThreadPool(10);

        executorservice.invokeAll(
                IntStream.range(0,5).boxed().map(
                        i-> (Callable <Integer> )() -> {
                            TimeUnit.SECONDS.sleep(5);
                            System.out.println(Thread.currentThread().getName() + " :" + i);
                            return i;
                        }
                        ).collect(Collectors.toList())
                ).parallelStream().map(future -> {
                try {
                    return  future.get();
                }catch (Exception e){
                    throw new RuntimeException(e);
                }
        }).forEach(System.out::println);
        System.out.println("================finished=======================");
    }

    /**
     * {@link ExecutorService#submit(Runnable,Object)}
     * @throws ExecutionException
     * @throws InterruptedException
     */
    private static void testSubmitRunnable() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Future<?> future = executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Object NULL = future.get();
        System.out.println("R:"+NULL);
    }



}
